﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpHelpers.TransferProtocols
{
    public class LTProtoBuffer
    {
        /// <summary>
        /// 返回固定消息
        /// </summary>
        /// <param name="megs"></param>
        /// <param name="parameterIndex">参数索引,从0开始</param>
        /// <returns></returns>
        public static NetworkMessage ConvertReceiveNetworkMessage(List<string> megs)
        {
            int valueStartIndex = 3 + Convert.ToInt32(megs[2]);

            NetworkMessage stru = new NetworkMessage();

            stru.CommandCode = Convert.ToInt32(megs[0]);
            stru.TranssferCode = Convert.ToInt32(megs[1]);
            stru.ParameterCount = Convert.ToInt32(megs[2]);
            stru.SourceType = Convert.ToInt32(megs[valueStartIndex]);
            stru.SourceCode = megs[valueStartIndex + 1];
            stru.DestinationType = Convert.ToInt32(megs[valueStartIndex + 2]);
            stru.DestinationCode = megs[valueStartIndex + 3];
            stru.MessageID = Convert.ToInt32(megs[valueStartIndex + 4]);

            return stru;
        }


        /// <summary>
        /// 返回Bool类型
        /// </summary>
        /// <param name="megs"></param>
        /// <param name="parameterIndex">参数索引,从0开始</param>
        /// <returns></returns>
        public static bool ConvertStringToBool(List<string> megs, int parameterIndex)
        {
            int realIndex = GetRealParameterIndex(megs, parameterIndex);

            bool result = (megs[realIndex] == "1") ? true : false;

            return result;
        }

        /// <summary>
        /// 返回Int32类型(对应4 Binary)
        /// </summary>
        /// <param name="megs"></param>
        /// <param name="parameterIndex">参数索引,从0开始</param>
        /// <returns></returns>
        public static Int32 ConvertStringToInt32(List<string> megs, int parameterIndex)
        {
            int realIndex = GetRealParameterIndex(megs, parameterIndex);

            Int32 result = Convert.ToInt32(megs[realIndex]);

            return result;
        }

        /// <summary>
        /// 返回String
        /// </summary>
        /// <param name="megs"></param>
        /// <param name="parameterIndex">参数索引,从0开始</param>
        /// <returns></returns>
        public static string ConvertString(List<string> megs, int parameterIndex)
        {
            int realIndex = GetRealParameterIndex(megs, parameterIndex);

            string result = megs[realIndex].Replace(",-1", "");
            if (result == "-1")
            {
                result = string.Empty;
            }

            return result;
        }

        public static Int32 GetRealParameterIndex(List<string> megs, int parameterIndex)
        {
            int parameterCount = Convert.ToInt32(megs[2]);
            parameterIndex = 3 + parameterCount + 5 + parameterIndex;

            return parameterIndex;
        }

        /// <summary>
        /// Int转特定长度Char字符串
        /// </summary>
        /// <param name="int16"></param>
        /// <param name="bits"></param>
        /// <returns></returns>
        public static string IntToString(int int16, int bits)
        {
            byte[] byteArray;
            int abc;
            int byteLenth;
            if (bits == 1)
            {
                byteArray = BitConverter.GetBytes((byte)int16);
            }
            else if (bits == 2)
            {
                byteArray = BitConverter.GetBytes((short)int16);
                abc = BitConverter.ToInt16(byteArray, 0);
            }
            else
            {
                byteArray = BitConverter.GetBytes(int16);
                abc = BitConverter.ToInt32(byteArray, 0);
            }

            if (bits == 1)
            {
                byteLenth = 1;
            }
            else
            {
                byteLenth = byteArray.Length;
            }


            //string str = UnicodeEncoding.ASCII.GetString(byteArray);
            string str = String.Empty;
            for (int i = 0; i < byteLenth; i++)
            {
                str += Convert.ToChar(byteArray[i]);
            }
            return str;
        }


        /// <summary>
        /// 16进制转ASCII（小端格式）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HexStringToAscii(string str)
        {
            byte[] buff = new byte[str.Length / 2];
            int index = 0;
            for (int i = str.Length - 2; i >= 0; i -= 2)
            {
                buff[index] = Convert.ToByte(str.Substring(i, 2), 16);
                index++;
            }

            string result = Encoding.Default.GetString(buff);

            return result;
        }

        public static string Byte4BinaryToString(string ascStr)
        {
            string reValue = string.Empty;

            for (int i = ascStr.Length / 8 - 1; i >= 0; i--)
            {
                string temp = string.Empty;
                for (int m = 0; m < 8; m++)
                {
                    temp += ascStr[i * 8 + m];
                }

                if (temp.ToLower() == "ffffffff")
                {
                    temp = "-1";
                }
                else
                {
                    temp = Change16To10(temp).ToString();
                }

                reValue += temp + ",";
            }

            if (reValue.Length > 0)
            {
                reValue = reValue.Substring(0, reValue.Length - 1);
            }

            return reValue;
        }

        public static int Change16To10(string message)
        {
            return Convert.ToInt32(message, 16);
        }

        public static byte[] StringTo16BitByLitteEndPoint(string ascStr, int len)
        {
            ascStr = ascStr.Substring(ascStr.Length - len * 4, len * 4);
            //ascStr = "004c0012008201010099010c0092005400220001009200190014001900190019001900010002";
            //ascStr = "5|0,9|0,12|0,12|0,12|0,12|0,2|0,12|0,6|9,9|0,5|8,2|8,6|9,3|0,12|0,9|0,5|9";
            int ascStrLength = ascStr.Length;

            byte[] vals = new byte[ascStrLength];

            for (int i = 0; i < ascStrLength; i++)
            {
                vals[i] = Convert.ToByte(ascStr[i]);
            }

            return vals;
        }

        /// <summary>
        /// 1102的特殊处理
        /// </summary>
        /// <param name="bt"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string ByteAcsiiToStringByCSharp(byte[] bt, int len)
        {
            string reValue = string.Empty;

            for (int i = len - 1; i >= 0; i--)
            {
                //
                byte[] vals = new byte[4];
                if (bt[i * 4] > 96)
                {
                    //针对小写a-f
                    vals[0] = Convert.ToByte(bt[i * 4] - 96 + 9);
                }
                else
                {
                    //针对小写0-9
                    vals[0] = Convert.ToByte(bt[i * 4] - 48);
                }

                if (bt[i * 4 + 1] > 96)
                {
                    vals[1] = Convert.ToByte(bt[i * 4 + 1] - 96 + 9);
                }
                else
                {
                    vals[1] = Convert.ToByte(bt[i * 4 + 1] - 48);
                }

                if (bt[i * 4 + 2] > 96)
                {
                    //针对小写a-f
                    vals[2] = Convert.ToByte(bt[i * 4 + 2] - 96 + 9);
                }
                else
                {
                    //针对小写0-9
                    vals[2] = Convert.ToByte(bt[i * 4 + 2] - 48);
                }

                if (bt[i * 4 + 3] > 96)
                {
                    vals[3] = Convert.ToByte(bt[i * 4 + 3] - 96 + 9);
                }
                else
                {
                    vals[3] = Convert.ToByte(bt[i * 4 + 3] - 48);
                }

                //vals[0]向左移4位到高八位
                int baccaratWinTpye = vals[2] << 4 | vals[3];

                if ((baccaratWinTpye & 0x01) != 0)
                {
                    //Tie
                    // baccaratWinTpye = 1;
                    if ((baccaratWinTpye & 0x08) != 0 && (baccaratWinTpye & 0x10) != 0)
                    {
                        baccaratWinTpye = 12;
                    }
                    else
                    {
                        if ((baccaratWinTpye & 0x08) != 0)
                        {
                            baccaratWinTpye = 11;
                        }
                        else if ((baccaratWinTpye & 0x10) != 0)
                        {
                            baccaratWinTpye = 10;
                        }
                        else
                        {
                            baccaratWinTpye = 9;
                        }
                    }

                }
                else if ((baccaratWinTpye & 0x02) != 0)
                {
                    //Player
                    // baccaratWinTpye = 2;
                    if ((baccaratWinTpye & 0x08) != 0 && (baccaratWinTpye & 0x10) != 0)
                    {
                        baccaratWinTpye = 8;
                    }
                    else
                    {
                        if ((baccaratWinTpye & 0x08) != 0)
                        {
                            baccaratWinTpye = 7;
                        }
                        else if ((baccaratWinTpye & 0x10) != 0)
                        {
                            baccaratWinTpye = 6;
                        }
                        else
                        {
                            baccaratWinTpye = 5;
                        }
                    }

                }
                else if ((baccaratWinTpye & 0x04) != 0)
                {
                    //Banker
                    //baccaratWinTpye = 4;
                    if ((baccaratWinTpye & 0x08) != 0 && (baccaratWinTpye & 0x10) != 0)
                    {
                        baccaratWinTpye = 4;
                    }
                    else
                    {
                        if ((baccaratWinTpye & 0x08) != 0)
                        {
                            baccaratWinTpye = 3;
                        }
                        else if ((baccaratWinTpye & 0x10) != 0)
                        {
                            baccaratWinTpye = 2;
                        }
                        else
                        {
                            baccaratWinTpye = 1;
                        }
                    }
                }

                //天牌
                //vals[3] = 0;
                int baccaratTianPai = vals[0] << 12 | vals[1] << 8 | vals[2] << 4 | 0x00;

                //baccaratTianPai = vals[3] << 12;

                if (i == 3)
                {
                    baccaratTianPai = vals[0] << 12 | vals[1] << 8 | vals[2] << 4 | 0x00;
                }
                string TianPai = "0";
                if ((baccaratTianPai & 32) != 0 || (baccaratTianPai & 64) != 0)
                {
                    //TianPai = "8";
                }
                else if ((baccaratTianPai & 128) != 0 || (baccaratTianPai & 256) != 0)
                {
                    //TianPai = "9";
                }

                reValue += baccaratWinTpye.ToString() + "|" + TianPai + ",";

                //if (len == (i + 1))
                //{
                //    break;
                //}
            }

            if (reValue.Length > 0)
            {
                reValue = reValue.Substring(0, reValue.Length - 1);
            }

            return reValue;
        }
    }
}
